package m.jclecai.com.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.adapter.LotteryHistoryAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.OpenListBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.inter.OnResultCallback;
import m.jclecai.com.utils.OkHttpClientManager;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.FastOk;

/**
 * Created by hp on 2017/3/14.
 * 开奖历史
 */

public class LotteryHistoryActivity extends BaseActivity implements
        AdapterView.OnItemClickListener,
        PullToRefreshBase.OnRefreshListener2 {

    @BindView(R.id.pr_listview)
    PullToRefreshListView prListview;

    @BindView(R.id.ll_klpk)
    LinearLayout mLlKlpk;

    @BindView(R.id.ll_ks)
    LinearLayout mLlKs;

    @BindView(R.id.view_title_line)
    View mViewTitleLine;

    private BaseAdapter adapter;

    @BindView(R.id.bt_bottom_right)
    Button bt_bottom_right;

    @BindView(R.id.bt_bottom_left)
    Button bt_bottom_left;

    private String type;

    List<OpenListBean.Records> mBean;

    int mPage = 1;

    @Override
    protected int getContentView() {
        return R.layout.lottery_history_activity;
    }

    @Override
    protected void initView() {
        setTitle("历史开奖");
        leftOptionText("");
        bt_bottom_right.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        final String typeName = intent.getStringExtra("typeName");
        if (!TextUtils.isEmpty(typeName)) {
            setTitle(typeName+"-历史开奖");
        }else{
            setTitle("历史开奖");
        }

        if (type.equals("ssq")) {
            bt_bottom_left.setText("双色球投注");
        } else if (type.equals("dlt")) {
            bt_bottom_left.setText("大乐透投注");
        } else if (type.equals("sd")) {
            bt_bottom_left.setText("福彩3D投注");
        } else if (type.equals("cqssc")) {
            bt_bottom_left.setText("时时彩投注");
        } else if (type.equals("qlc")) {
            bt_bottom_left.setText("七乐彩投注");
        } else if (type.equals("qxc")) {
            bt_bottom_left.setText("七星彩投注");
        } else if (type.equals("plw")) {
            bt_bottom_left.setText("排列五投注");
        } else if (type.equals("pls")) {
            bt_bottom_left.setText("排列三投注");
        } else if (type.equals("sdsyxw")) {
            bt_bottom_left.setText("山东11选5投注");
        } else if (type.equals("syxw")) {
            bt_bottom_left.setText("江西11选5投注");
        } else if (type.equals("sfc")) {
            bt_bottom_left.setText("胜负彩投注");
        }
        showLoadingLayout(prListview);
        mBean = new ArrayList<>();

        if (type.contains("klpk")) {
            //这里的话，我们是需要创建一个klpk的Adpater;
            adapter = new KLPKHistoryAdapter(this, mBean);
            mLlKlpk.setVisibility(View.VISIBLE);
            mViewTitleLine.setVisibility(View.VISIBLE);
            mLlKs.setVisibility(View.GONE);

        }else if(type.contains("ks")){

            adapter = new KSHistoryAdapter(this, mBean);
            mLlKlpk.setVisibility(View.GONE);
            mViewTitleLine.setVisibility(View.VISIBLE);
            mLlKs.setVisibility(View.VISIBLE);

        } else {
            adapter = new LotteryHistoryAdapter(this, mBean);

            mLlKlpk.setVisibility(View.GONE);
            mViewTitleLine.setVisibility(View.GONE);
            mLlKs.setVisibility(View.GONE);
        }

        prListview.setAdapter(adapter);

        prListview.setOnRefreshListener(this);
        getOpenListData(type);

        prListview.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //低频彩是可以查看详情的；
               if("ssq".equals(type)
                        || "dlt".equals(type)
                        || "qxc".equals(type)
                        || "qlc".equals(type)
                        || "sd".equals(type)
                        || "pls".equals(type)
                        || "plw".equals(type)
                       ){
                   LotteryHistoryAdapter.ViewHolder viewHolder = (LotteryHistoryAdapter.ViewHolder) view.getTag();
                   Intent intent = new Intent(LotteryHistoryActivity.this, LotteryDetailsActivity.class);
                    intent.putExtra("type", type);
                    intent.putExtra("term", viewHolder.tv_ball_two.getTag().toString());
                    intent.putExtra("date",viewHolder.tv_ball_one.getTag().toString());
                    intent.putExtra("typeName","开奖详情");
                    startActivity(intent);
                }
            }
        });
    }

    private void getOpenListData(String type) {
        OkHttpClientManager.Param Url = new OkHttpClientManager.Param("url", Constant_Url.OPEN_LIST);
        OkHttpClientManager.Param Type = new OkHttpClientManager.Param("type", type);
        OkHttpClientManager.Param page = new OkHttpClientManager.Param("page", String.valueOf(mPage));
        OkHttpClientManager.Param[] param = new OkHttpClientManager.Param[]{Url, Type, page};
        try {
            OkHttpClientManager.getInstance().asynPost(new OnResultCallback() {
                @Override
                public void onGetResult(String json) {
                    closeLoadingLayout();
                    if (prListview != null)
                        prListview.onRefreshComplete();
                    OpenListBean bean = FastOk.get(json, OpenListBean.class);
                    if (bean != null && bean.getRecords() != null && bean.getRecords().size() > 0) {
                        mPage++;
                        mBean.addAll(bean.getRecords());
                    }
                    if (mBean != null && mBean.size() <= 0)
                        showEmptyLayout();
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onError() {
                    if (prListview != null)
                        prListview.onRefreshComplete();
                    showInternetOffLayout();
                    ToastUtil.showToast(LotteryHistoryActivity.this, R.string.connect_failure);
                }
            }, param);
        } catch (IOException e) {
            dissDialog();
            e.printStackTrace();
        }
    }

    @OnClick(R.id.bt_bottom_left)
    public void onClick(View v) {
        if (type.equals("ssq") || type.equals("dlt") || type.equals("qlc")) {
            Intent intent = new Intent(this, BetSSQ_DLT_QLCActivity.class);
            intent.putExtra("type", type);
            startActivity(intent);
        } else if (type.equals("sfc") || type.equals("rxj")) {
            Intent intent = new Intent(this, BetRXJ_SFCActivity.class);
            intent.putExtra("type", type);
            startActivity(intent);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView tv_ball_one = (TextView) view.findViewById(R.id.tv_ball_one);
        String date = tv_ball_one.getTag().toString();
        TextView tv_ball_two = (TextView) view.findViewById(R.id.tv_ball_two);
        String term = tv_ball_two.getTag().toString();
        TextView tv_ball_three = (TextView) view.findViewById(R.id.tv_ball_three);
        String type = tv_ball_three.getTag().toString();
        if (type.equals("sfc")) {
            Intent intent = new Intent(this, SFC_LotteryDetails.class);
            intent.putExtra("term", term);
            intent.putExtra("type", type);
            startActivity(intent);
        } else {
            Intent intent = new Intent(this, LotteryDetailsActivity.class);
            intent.putExtra("date", date);
            intent.putExtra("term", term);
            intent.putExtra("type", type);
            startActivity(intent);
        }
    }

    @Override
    public void OnRetry(String btnMessage) {
        mPage = 1;
        showLoadingLayout(prListview);
        getOpenListData(type);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        //下拉
        mBean.clear();
        mPage = 1;
        getOpenListData(type);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        getOpenListData(type);
    }


}
